package com.henanuniversity.dao.impl;

import com.henanuniversity.dao.CartDao;
import com.henanuniversity.po.Business;
import com.henanuniversity.po.Cart;
import com.henanuniversity.po.Food;
import com.henanuniversity.utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;
    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql=new StringBuffer();
        sql.append(" select c.*, ");
        sql.append(" f.foodId ffoodId, ");
        sql.append(" f.foodName ffoodName, ");
        sql.append(" f.foodExplain ffoodExplain, ");
        sql.append(" f.foodImg ffoodImg, ");
        sql.append(" f.foodPrice ffoodPrice, ");
        sql.append(" f.businessId fbusinessId, ");
        sql.append(" f.remarks fremarks, ");
        sql.append(" b.businessId bbusinessId, ");
        sql.append(" b.businessName bbusinessName, ");
        sql.append(" b.businessAddress bbusinessAddress, ");
        sql.append(" b.businessExplain bbusinessExplain, ");
        sql.append(" b.businessImg bbusinessImg, ");
        sql.append(" b.orderTypeId borderTypeId, ");
        sql.append(" b.starPrice bstarPrice, ");
        sql.append(" b.deliveryPrice bdeliveryPrice ");
        sql.append(" from cart c left join food f on c.foodId=f.foodId left join business  b on c.businessId=b.businessId ");
        sql.append(" where 1=1 ");
        if(cart.getBusinessId()!=null){
            sql.append(" and c.businessId="+cart.getBusinessId());
        }
        if(cart.getUserId()!=null){
            sql.append(" and c.userId='"+cart.getUserId()+"'");
        }
        System.out.println(sql.toString());
        try {
            conn= DBUtils.getConnection();
            pst=conn.prepareStatement(sql.toString());
            rs=pst.executeQuery();
            while(rs.next()){
                Cart cart1 = new Cart();
                cart1.setCartId(rs.getInt("cartId"));
                cart1.setFoodId(rs.getInt("foodId"));
                cart1.setBusinessId(rs.getInt("businessId"));
                cart1.setUserId(rs.getString("userId"));
                cart1.setQuantity(rs.getInt("quantity"));
                Food food = new Food();
                food.setFoodId(rs.getInt("ffoodId"));
                food.setFoodName(rs.getString("ffoodName"));
                food.setFoodExplain(rs.getString("ffoodExplain"));
                food.setFoodImg(rs.getString("ffoodImg"));
                food.setFoodPrice(rs.getDouble("ffoodPrice"));
                food.setBusinessId(rs.getInt("fbusinessId"));
                food.setRemarks(rs.getString("fremarks"));
                cart1.setFood(food);

                Business business = new Business();
                business.setBusinessId(rs.getInt("bbusinessId"));
                business.setBusinessName(rs.getString("bbusinessName"));
                business.setBusinessExplain(rs.getString("bbusinessExplain"));
                business.setBusinessImg(rs.getString("bbusinessImg"));
                business.setBusinessAddress(rs.getString("bbusinessAddress"));
                business.setOrderTypeId(rs.getInt("borderTypeId"));
                business.setStarPrice(rs.getDouble("bstarPrice"));
                business.setDeliveryPrice(rs.getDouble("bdeliveryPrice"));
                cart1.setBusiness(business);

                list.add(cart1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return list;
    }

    @Override
    public int saveCart(Cart cart) {
        int result=0;
        String sql="insert into cart values(null,?,?,?,1)";
        try {
            conn= DBUtils.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setInt(1,cart.getFoodId());
            pst.setInt(2,cart.getBusinessId());
            pst.setString(3,cart.getUserId());
            //pst.setInt(4,cart.getQuantity());
            result=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return result;
    }

    @Override
    public int removeCart(Cart cart) {
        int result=0;
        StringBuffer sql=new StringBuffer("delete from cart where 1=1");
        if(cart.getFoodId()!=null){
            sql.append(" and foodId="+cart.getFoodId());
        }
        if(cart.getBusinessId()!=null){
            sql.append(" and businessId="+cart.getBusinessId());
        }
        if(cart.getUserId()!=null){
            sql.append(" and userId='"+cart.getUserId()+"'");
        }
        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql.toString());
            result=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) {
        int result=0;
        String sql="update cart set quantity=? where foodId=?  and businessId=? and userId=?" ;

        try {
            conn=DBUtils.getConnection();
            pst=conn.prepareStatement(sql.toString());
            pst.setInt(1,cart.getQuantity());
            pst.setInt(2,cart.getFoodId());
            pst.setInt(3,cart.getBusinessId());
            pst.setString(4,cart.getUserId());
            result=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
             DBUtils.close(rs,pst);
        }
        return result;
    }
}
